package views;

/**
 *
 * @author sebastian
 */
import logicBusiness.AsignaturaBL;
import logicBusiness.AsignaturaHoraBL;
import logicBusiness.HoraBL;
import javax.swing.DefaultComboBoxModel;
import logicBusiness.GradoBL;
import models.Asignatura;
//import models.AsignaturaHora;
//import models.Hora;
import models.Grado;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class InterfazAsociarAsignaturaHora extends javax.swing.JFrame {

    private GradoBL boG = new GradoBL();
    private HoraBL boH = new HoraBL();
    private AsignaturaBL boA = new AsignaturaBL();
    private Grado g = new Grado();
//    private Hora h = new Hora();
//    private AsignaturaHora ah = new AsignaturaHora();
    private int ok = 0;
    private AsignaturaHoraBL boAH = new AsignaturaHoraBL();

    /** Creates new form InterfazAsociarAsignaturaHora */
    public InterfazAsociarAsignaturaHora() {
        initComponents();
        cargarComboGrados();
        cargarComboHoras();
        cargarListaAsignaturas();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        etiqueta1 = new javax.swing.JLabel();
        etiqueta2 = new javax.swing.JLabel();
        comboGrados = new javax.swing.JComboBox();
        etiqueta3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaAsignaturas = new javax.swing.JList();
        etiqueta4 = new javax.swing.JLabel();
        comboHoras = new javax.swing.JComboBox();
        etiqueta5 = new javax.swing.JLabel();
        b_asociar = new javax.swing.JButton();
        b_desasociar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaAsignaturasHoras = new javax.swing.JList();
        b_volver = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        etiqueta1.setText("Asociar asignaturas con horas");

        etiqueta2.setText("Escoja grado:");

        comboGrados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                comboGradosMousePressed(evt);
            }
        });
        comboGrados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboGradosActionPerformed(evt);
            }
        });

        etiqueta3.setText("Escoja asignatura:");

        listaAsignaturas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                listaAsignaturasMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(listaAsignaturas);

        etiqueta4.setText("Escoja hora:");

        etiqueta5.setText("Horas en las que se dicta la asignatura:");

        b_asociar.setText("Asociar");
        b_asociar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_asociarActionPerformed(evt);
            }
        });

        b_desasociar.setText("Desasociar");
        b_desasociar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_desasociarActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(listaAsignaturasHoras);

        b_volver.setText("Volver");
        b_volver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_volverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(156, 156, 156)
                        .addComponent(b_volver)
                        .addGap(228, 228, 228))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(etiqueta5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 363, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(etiqueta3)
                                    .addGap(18, 18, 18)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(76, 76, 76)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(b_desasociar)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(etiqueta4)
                                                .addGap(18, 18, 18)
                                                .addComponent(comboHoras, 0, 173, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(b_asociar)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(etiqueta2)
                                            .addGap(18, 18, 18)
                                            .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(etiqueta1))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 408, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(62, 62, 62))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {b_asociar, b_desasociar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(etiqueta1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(etiqueta2)
                    .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(etiqueta4)
                            .addComponent(comboHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(38, 38, 38)
                        .addComponent(b_asociar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(b_desasociar))
                    .addComponent(etiqueta3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(etiqueta5)
                        .addGap(36, 36, 36)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(22, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(b_volver)
                        .addGap(91, 91, 91))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_volverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_volverActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_b_volverActionPerformed

    private void comboGradosMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboGradosMousePressed
        // cargarListaAsignaturas();
    }//GEN-LAST:event_comboGradosMousePressed

    private void b_asociarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_asociarActionPerformed
        if (comboHoras.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una hora para asociar", null, JOptionPane.ERROR_MESSAGE);
        } else if (listaAsignaturas.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una asignatura para asociar", null, JOptionPane.ERROR_MESSAGE);
        }
        Asignatura as = new Asignatura();
        Integer idAsignatura = 0;
        idAsignatura = boA.encontrarAsignatura(listaAsignaturas.getSelectedValue() + "");
        String txt = comboHoras.getSelectedItem() + "";
        ok = boAH.validarCreacion(idAsignatura, txt);
        if (ok == 1) {
            JOptionPane.showMessageDialog(this, "Lo sentimos la asignatura seleccionada ya ha sifo asociada a la hora especificado", null, JOptionPane.ERROR_MESSAGE);
        } else {
//            boAH.asociarAsignaturaDia(idAsignatura, txt);
            JOptionPane.showMessageDialog(this, "La asignacion se ha realizado exitosamente", null, JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_b_asociarActionPerformed

    private void listaAsignaturasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaAsignaturasMousePressed
        if (listaAsignaturas.getSelectedIndex() != -1) {
            ok = boA.encontrarAsignatura(listaAsignaturas.getSelectedValue() + "");
//            List l = boAH.consultarHorasAsociadas(ok);
            DefaultListModel modelo = new DefaultListModel();
//            for (int i = 0; i < l.size(); i++) {
//                modelo.addElement(l.get(i) + "");
//            }
            listaAsignaturasHoras.setModel(modelo);
        }
    }//GEN-LAST:event_listaAsignaturasMousePressed

    private void b_desasociarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_desasociarActionPerformed
        if (listaAsignaturas.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una Hora para desasociar", null, JOptionPane.ERROR_MESSAGE);
        } else if (listaAsignaturasHoras.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una asignatura para desasociar", null, JOptionPane.ERROR_MESSAGE);
        } else {
            Asignatura as = new Asignatura();
            Integer idAsignatura = 0;
            idAsignatura = boA.encontrarAsignatura(listaAsignaturas.getSelectedValue() + "");
//            h = boH.consultarHora(listaAsignaturasHoras.getSelectedValue() + "");
//            String nombreHora = h.getNombreHora();
//            ah = boAH.devolverAsignaturaHora(idAsignatura, nombreHora);
//            if (ah != null) {
//                boAH.desasociarAsignaturaDia(ah);
//                JOptionPane.showMessageDialog(this, "se ha desasociado el dia seleccionado a la asignatura especificada exitosamente", null, JOptionPane.INFORMATION_MESSAGE);
//            } else {
//                JOptionPane.showMessageDialog(this, "Ocurrio un error al desasociar el dia seleccionado con la asignatura especificada", null, JOptionPane.ERROR_MESSAGE);
//            }
        }
    }//GEN-LAST:event_b_desasociarActionPerformed

    private void comboGradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboGradosActionPerformed
        cargarListaAsignaturas();
    }//GEN-LAST:event_comboGradosActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazAsociarAsignaturaHora().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_asociar;
    private javax.swing.JButton b_desasociar;
    private javax.swing.JButton b_volver;
    private javax.swing.JComboBox comboGrados;
    private javax.swing.JComboBox comboHoras;
    private javax.swing.JLabel etiqueta1;
    private javax.swing.JLabel etiqueta2;
    private javax.swing.JLabel etiqueta3;
    private javax.swing.JLabel etiqueta4;
    private javax.swing.JLabel etiqueta5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listaAsignaturas;
    private javax.swing.JList listaAsignaturasHoras;
    // End of variables declaration//GEN-END:variables

    private void cargarComboGrados() {
        List l = boG.consultarGrados();
        DefaultComboBoxModel modelo = new DefaultComboBoxModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));

        }
        comboGrados.setModel(modelo);
    }

    private void cargarComboHoras() {
        List l = boH.consultarHoras();
        DefaultComboBoxModel modelo = new DefaultComboBoxModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));
        }
        comboHoras.setModel(modelo);
    }

    private void cargarListaAsignaturas() {
        int idGrado = comboGrados.getSelectedIndex();
        List l = boA.consultarAsignaturasGrado(idGrado);
        DefaultListModel dlm = new DefaultListModel();
        for (int i = 0; i
                < l.size(); i++) {
            dlm.addElement(l.get(i) + "");
        }
        listaAsignaturas.setModel(dlm);
    }
}
